<template>
<div class="com-container" style="margin: 20px auto;width: 45%">
  <el-form ref="form" :model="stuInfo" :rules="rules" label-width="auto" :inline="true">
    <el-form-item label="姓名" prop="name">
      <el-input v-model="stuInfo.name"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-input v-model="stuInfo.sex"></el-input>
    </el-form-item>
    <el-form-item label="民族" prop="nation">
      <el-input v-model="stuInfo.nation"></el-input>
    </el-form-item>
    <el-form-item label="年龄" prop="age">
      <el-input v-model="stuInfo.age"></el-input>
    </el-form-item>
    <el-form-item label="出生日期" prop="birthdate">
      <el-input v-model="stuInfo.birthdate"></el-input>
    </el-form-item>
    <el-form-item label="政治面貌" prop="policalstatus">
      <el-input v-model="stuInfo.policalstatus"></el-input>
    </el-form-item>
    <el-form-item label="家庭住址" prop="address">
      <el-input v-model="stuInfo.address"></el-input>
    </el-form-item>
    <el-form-item label="毕业高中" prop="graducatedschool">
      <el-input v-model="stuInfo.graducatedschool"></el-input>
    </el-form-item>
    <el-form-item label="高考分数" prop="gkscore">
      <el-input v-model="stuInfo.gkscore"></el-input>
    </el-form-item>
    <el-form-item label="电话" prop="phone">
      <el-input v-model="stuInfo.phone"></el-input>
    </el-form-item>
    <el-form-item label="qq" prop="qq">
      <el-input v-model="stuInfo.qq"></el-input>
    </el-form-item>
    <el-form-item label="微信" prop="wechat">
      <el-input v-model="stuInfo.wechat"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">确定</el-button>
    </el-form-item>
  </el-form>
</div>
</template>

<script>
export default {
  name: "InformationComplete",
  data() {

    // 性别验证
    var checkSex = (rule, value, callback) => {
      const c = /^['男','女']{1}$/
      if (!c.test(value)) {
        return callback(new Error("请输入正确的性别"));
      } else {
        return callback()
      }
    }
    // 年龄验证
    var checkAge = (rule, value, callback) => {
      const d = /^\+?[1-9][0-9]*$/
      if (!d.test(value)) {
        return callback(new Error("请输入正确的年龄格式"));
      } else if (value < 6) {
        return callback(new Error("建议去隔壁幼儿园亲"));
      } else if (value > 120) {
        return callback(new Error("年纪是不是太大了捏"));
      }
      else {
        return callback()
      }
    }
    // 出生日期验证
    var checkBirthdate = (rule, value, callback) => {
      const e = /^([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))$/
      if (!e.test(value)) {
        return callback(new Error("仅支持YYYY-MM-DD的日期格式"));
      } else {
        return callback()
      }
    }
    // 高考分数验证
    var checkGkScore = (rule, value, callback) => {
      if (value == null || String(value).trim() === "") {
        return callback()
      }
      const d = /^\d{3}$/
      if (!d.test(value)) {
        return callback(new Error("请输入正确的高考分数格式"));
      } else {
        return callback()
      }
    }
    // 电话验证
    var checkPhone = (rule, value, callback) => {
      if (value == null || String(value).trim() === "") {
        return callback()
      }
      const d = /^\d{11}$/
      if (!d.test(value)) {
        return callback(new Error("请输入正确的电话号码格式"));
      } else {
        return callback()
      }
    }
    // qq验证
    var checkQq = (rule, value, callback) => {
      if (value == null || String(value).trim() === "") {
        return callback()
      }
      const d = /^[0-9]{5,}$/
      if (!d.test(value)) {
        return callback(new Error("请输入正确的电话号码格式"));
      } else {
        return callback()
      }
    }
    // 微信验证
    var checkWeChat = (rule, value, callback) => {
      if (value == null || String(value).trim() === "") {
        return callback()
      }
      const d = /^\w{0,}$/
      if (!d.test(value)) {
        return callback(new Error("请输入正确的微信号格式"));
      } else {
        return callback()
      }
    }
    return {
      stuInfo: {
        id: JSON.parse(localStorage.getItem("user")).id,
        name: JSON.parse(localStorage.getItem("user")).name,
        sex: JSON.parse(localStorage.getItem("user")).sex,
        nation: JSON.parse(localStorage.getItem("user")).nation
      },
      rules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'},
          {min: 2, max: 10, message: '检测到异常姓名长度', trigger: 'blur'},
        ],
        sex: [
          {required: true, message: '请输入性别', trigger: 'blur'},
          {validator: checkSex, trigger: 'blur'}
        ],
        age: [
          {required: true, message: '请输入年龄', trigger: 'blur'},
          {validator: checkAge, trigger: 'blur'}
        ],
        birthdate: [
          {required: true, message: '请输入出生日期', trigger: 'blur'},
          {validator: checkBirthdate, trigger: 'blur'}
        ],

        gkscore: [
          {validator: checkGkScore, trigger: 'blur'}
        ],
        phone: [
          {validator: checkPhone, trigger: 'blur'}
        ],
        qq: [
          {validator: checkQq, trigger: 'blur'}
        ],
        wechat: [
          {validator: checkWeChat, trigger: 'blur'}
        ],
      }

    }
  },
  methods: {
    onSubmit() {
      this.request.put("/student",this.stuInfo).then(res=>{
        if (res.code==20031) {
          this.$message.success("个人信息已完善，请继续点击下一步完成签到")
        } else if(res.code==20030) {
          this.$message.error(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      })
    },
  }
}
</script>

<style scoped>

</style>